# Maintainer: LH_Mouse <lh_mouse@126.com>

_realname=mcfgthread
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}"-libs)
pkgver=2.1.1
pkgrel=1
pkgdesc="An efficient gthread implementation, providing C++11 threading support (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/lhmouse/mcfgthread"
license=('spdx:LGPL-3.0-or-later WITH GCC-exception-3.1')
options=('!buildflags')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-crt"
  "${MINGW_PACKAGE_PREFIX}-headers"
  "git"
)
source=("git+https://github.com/lhmouse/mcfgthread.git#tag=v${pkgver%.*}-ga.${pkgver##*.}")
sha256sums=('a2a38a6875bbe713b08027dbe39a5daa0e72206f2eee3a5713929ed5716d417f')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
    --prefix="${MINGW_PREFIX}" \
    -Dbuildtype=plain  \
    -Dc_args='-Os -g' \
    -Dc_link_args='-Os'  \
    . \
    ../${_realname}

  # Build the DLL first, as Meson creates the DLL in the working directory,
  # which might get picked up by cc1.exe when it is only half-baked.
  ninja libmcfgthread-${pkgver%%.*}.dll
  meson compile

  rm -rf "${srcdir}${MINGW_PREFIX}"
  DESTDIR="${srcdir}" meson install
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  meson test
}

package_mcfgthread() {
  pkgdesc+=" (devel)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}")

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  cp --parents -r include -t "${pkgdir}${MINGW_PREFIX}"
  cp --parents -r lib -t "${pkgdir}${MINGW_PREFIX}"
}

package_mcfgthread-libs() {
  pkgdesc+=" (libs)"

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  cp --parents -r bin/libmcfgthread-${pkgver%%.*}.dll -t "${pkgdir}${MINGW_PREFIX}"
  cp --parents -r bin/libmcfgthread-minimal-${pkgver%%.*}.dll -t "${pkgdir}${MINGW_PREFIX}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
